package com.huawei.app.demo.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import android.content.Context;
import android.content.Intent;

import com.huawei.app.demo.MainActivity;
import com.huawei.app.httpapi.ThreadPools;

/**
 * 显示隐藏系统图标
 *
 * @since 2021-01-20
 */
public class SystemIconUtils {

    private static final String TAG = SystemIconUtils.class.getSimpleName();

    /**
     * 隐藏所有图标
     * @param context
     */
    public static void hideAllIcon(final Context context) {
        hideNinePoint(context, true);
        hideToolbar(context, true);
        hideBackBtn(context, true);
    }

    /**
     * 隐藏九宫格
     * @param context
     * @param isHide
     */
    public static void hideNinePoint(Context context, boolean isHide) {
        Map<String, Object> params = new HashMap<>();
        params.put("packageName", MainActivity.ACTIVITY_PACKAGE_NAME);
        params.put("state", isHide);

        sendBroadcast(
                context, "com.device.action.NOTIFY_NINE_POINT_SHOW_HIDE",
                params, "com.device.permission.THIRDAPP");
    }

    /**
     * 隐藏返回键
     * @param context
     * @param isHide
     */
    public static void hideBackBtn(Context context, boolean isHide) {
        Map<String, Object> params = new HashMap<>();
        params.put("packageName", MainActivity.ACTIVITY_PACKAGE_NAME);
        params.put("state", isHide);

        sendBroadcast(
                context, "com.device.action.NOTIFY_BACK_BTN_SHOW_HIDE",
                params, "com.device.permission.THIRDAPP");
    }

    /**
     * 隐藏工具栏
     * @param context
     * @param isHide
     */
    public static void hideToolbar(Context context, boolean isHide) {
        Map<String, Object> params = new HashMap<>();
        params.put("packageName", MainActivity.ACTIVITY_PACKAGE_NAME);
        params.put("state", isHide);

        sendBroadcast(
                context, "com.device.action.NOTIFY_TOOLBAR_SHOW_HIDE",
                params, "com.device.permission.THIRDAPP");
    }

    /**
     * 发送广播消息
     * @param context
     * @param broadcastName
     * @param params
     * @param permission
     * @return
     */
    private static boolean sendBroadcast(Context context, String broadcastName, Map<String, Object> params, String permission) {
        Intent intent = new Intent(broadcastName);
        Iterator<Map.Entry<String,Object>> it = params.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String,Object> entry = it.next();
            String key = entry.getKey();
            Object val = entry.getValue();
            if (val instanceof Boolean) {
                intent.putExtra(key, (Boolean)val);
            } else if (val instanceof String) {
                intent.putExtra(key, (String)val);
            } else if (val instanceof Integer) {
                intent.putExtra(key, (Integer)val);
            }
        }

        boolean sendSuccess = false;
        try {
            context.sendBroadcast(intent, permission);
            sendSuccess = true;
        } catch (Exception e) {
            LogUtils.getInstance().e(TAG, "sendBroadcast failed", e);
        }
        return sendSuccess;
    }
}
